home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DESDOS.ARJ / UUDECODE.C < prev    next >
C/C++ Source or Header  |  1989-01-30  |  2KB  |  69 lines

  1. /* uudecode.c - convert ascii-encoded files back to their original form
  2.  * Usage: uudecode [infile]
  3.  *
  4.  * This command differs from the regular UNIX one in that the embedded
  5.  * file name "/dev/stdout" is recognized, allowing it to be used in a pipeline
  6.  *
  7.  * Written and placed in the public domain by Phil Karn, KA9Q 31 March 1987
  8.  */
  9. #include <stdio.h>
  10. #define    LINELEN    80
  11. main(argc,argv)
  12. int argc;
  13. char *argv[];
  14. {
  15.     char linebuf[LINELEN],*index(),*fgets();
  16.     register char *cp;
  17.     int linelen,i;
  18.     FILE *in,*out;
  19.     
  20.     if(argc > 1){
  21.         if((in = fopen(argv[1],"r")) == NULL){
  22.             fprintf(stderr,"Can't read %s\n",argv[1]);
  23.             exit(1);
  24.         }
  25.     } else
  26.         in = stdin;
  27.  
  28.     /* Find begin line */
  29.     while(fgets(linebuf,LINELEN,in) != NULL){
  30.         if((cp = index(linebuf,'\n')) != NULL)
  31.             *cp = '\0';
  32.         if(strncmp(linebuf,"begin",5) == 0)
  33.             break;
  34.     }
  35.     if(feof(in)){
  36.         fprintf(stderr,"No begin found\n");
  37.         exit(1);
  38.     }
  39.     /* Find beginning of file name */
  40.     cp = &linebuf[6];
  41.     if((cp = index(cp,' ')) != NULL)
  42.         cp++;
  43.     /* Set up output stream */
  44.     if(cp == NULL || strcmp(cp,"/dev/stdout") == 0){
  45.         out = stdout;
  46.     } else if((out = fopen(cp,"w")) == NULL){
  47.             fprintf(stderr,"Can't open %s\n",cp);
  48.             exit(1);
  49.     }
  50.     /* Now crunch the input file */
  51.     while(fgets(linebuf,LINELEN,in) != NULL){
  52.         linelen = linebuf[0] - ' ';
  53.         if(linelen == 0 || strncmp(linebuf,"end",3) == 0)
  54.             break;
  55.         for(cp = &linebuf[1];linelen > 0;cp += 4){
  56.             for(i=0;i<4;i++)
  57.                 cp[i] -= ' ';
  58.             putc((cp[0] << 2) | ((cp[1] >> 4) & 0x3),out);
  59.             if(--linelen > 0)
  60.                 putc((cp[1] << 4) | ((cp[2] >> 2) & 0xf),out);
  61.             if(--linelen > 0)
  62.                 putc((cp[2] << 6) | cp[3],out);
  63.             linelen--;
  64.         }
  65.     }
  66.     fclose(out);
  67. }
  68.  
  69.